<template>
  <span class="notice-button" style="padding-right:10px" v-if="!data">
    <Badge :count="count" style="padding-right:4px;">
      <Icon type="ios-bell-outline" class="icon"></Icon>
    </Badge>
  </span>
  <Poptip placement="bottom-end" width="336" class="notice-pop" v-else>
    <span class="notice-button" style="padding-right:10px">
      <Badge :count="count" style="padding-right:4px;">
        <Icon type="ios-bell-outline" class="icon"></Icon>
      </Badge>
    </span>
    <div slot="content">
      <Tabs v-model="currentTab">
        <TabPane :label="item.title" v-for="(item, index) in data" :name="index.toString()" :key="index">
          <div v-if="currentTab === index.toString()">
            <div class="not-found" v-if="item.msg.length === 0">
              <img :src="item.emptyImage? item.emptyImage : locale.emptyImage" alt="not found">
              <div>{{ item.emptyText ? item.emptyText : locale.emptyText }}</div>
            </div>
            <div class="list">
              <NoticeList v-for="(message, index) in item.msg" :avatar="message.avatar" :key="index" :title="message.title" :description="message.description ? message.description : ''" :datetime="message.datetime" :extra="message.extra" :color="message.status ? colors[message.status] : ''" :read="message.read" :index="index" @onItemClick="handleClick"></NoticeList>
            </div>
            <div class="clear" @click="handleClear(index)" v-if="item.msg.length>0">
              {{locale.clear}}{{ item.title }}
            </div>
          </div>
        </TabPane>
      </Tabs>
    </div>
  </Poptip>
</template>
<script>
import NoticeList from './NoticeList'
export default {
  name: 'NoticeIcon',
  components: { NoticeList },
  data() {
    return {
      currentTab: '0'
    }
  },
  props: {
    count: {
      type: Number
    },
    data: {
      type: Array
    },
    colors: {
      type: Object
    },
    locale: {
      type: Object,
      default: () => {
        return {
          emptyText: '暂无数据',
          clear: '清空',
          emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg'
        }
      }
    }
  },
  methods: {
    unReadCount(item) {
      let count = 0
      item.msg.forEach(msg => {
        !msg.read && count++
      })
      return count
    },
    handleClear(index) {
      this.$emit('onClear', index)
    },
    handleClick(index) {
      this.$emit('onItemClick', index, parseInt(this.currentTab))
    }
  }
}
</script>
<style scoped>
.notice-button {
  cursor: pointer;
  display: inline-block;
  transition: all 0.3s;
}

.not-found {
  text-align: center;
  padding: 73px 0 88px 0;
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;
}

.not-found img {
  display: inline-block;
  margin-bottom: 16px;
  height: 76px;
}

.icon {
  font-size: 22px;
  vertical-align: middle;
}

.list {
  max-height: 400px;
  overflow: auto;
}

.clear {
  font-size: 14px;
  height: 46px;
  line-height: 46px;
  text-align: center;
  color: rgba(0, 0, 0, 0.65);
  border-radius: 0 0 4px 4px;
  border-top: 1px solid #e8e8e8;
  -webkit-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
  cursor: pointer;
  margin-top: -1px;
}

.clear:hover {
  color: rgba(0, 0, 0, 0.85);
}
</style>
<style>
.notice-pop .ivu-poptip-body {
  padding: 0;
}

.notice-pop .ivu-tabs-nav {
  float: none;
  display: inline-block;
}

.notice-pop .ivu-badge-count {
  top: -12px;
  user-select: none;
}

.notice-pop .ivu-tabs-nav-scroll {
  text-align: center;
}

.notice-pop .ivu-tabs-tab {
  padding: 12px 16px;
}

.notice-pop .ivu-tabs-bar {
  margin-bottom: 8px;
}

.notice-pop .ivu-tabs-tab:last-child {
  margin-right: 0;
}

.notice-pop .ivu-tabs-nav-prev,
.notice-pop .ivu-tabs-nav-next {
  line-height: 45px;
}

.notice-pop .ivu-tabs-nav-prev {
  left: 8px;
}

.notice-pop .ivu-tabs-nav-next {
  right: 8px;
}
</style>
